package com.jn.erp.web.api.institution.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jn.erp.common.annotation.Log;
import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.common.enums.BusinessType;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.security.entity.SysRole;
import com.jn.erp.security.sysservice.SysRoleService;
import com.jn.erp.security.sysservice.SysUserRoleService;
import com.jn.erp.security.sysservice.SysUserService;
import com.jn.erp.web.api.institution.entity.vo.EmployeeInfoDto;
import com.jn.erp.web.api.institution.entity.vo.EmployeeVo;
import com.jn.erp.web.api.institution.service.EmployeeService;
import com.jn.erp.web.api.sys.entity.SysOrgUser;
import com.jn.erp.web.api.sys.service.SysOrgUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <pre>
 *  机构管理-员工管理
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/2 8:58
 */
@RestController
@RequestMapping("/institution/staff")
public class StaffController extends BaseWebAuthController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private SysOrgUserService sysOrgUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysUserService sysUserService;

//-----------------------------------------   用户管理-----------------------------
    /**
     * 机构员工列表
     */
//    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/employees")
    public TableDataInfo list(EmployeeVo employeeVo) {
        String orgId = loginDeptAuth();
        employeeVo.setOrgId(Long.valueOf(orgId));
        startPage();
        List<EmployeeVo> list = employeeService.selectEmployeeList(employeeVo);
        return getDataTable(list);
    }

    /**
     * 机构新增员工
     */
//    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "机构新增员工", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@Validated @RequestBody EmployeeVo employeeVo) {
        String orgId = loginDeptAuth();
        employeeVo.setOrgId(Long.valueOf(orgId));
        return employeeService.employeeUnifyAdd(employeeVo);
    }

    /**
     * 修改员工
     */
//    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "机构修改员工", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@Validated @RequestBody EmployeeVo employeeVo) {
        String orgId = loginDeptAuth();
        employeeVo.setOrgId(Long.valueOf(orgId));
        return employeeService.employeeUnifyEdit(employeeVo);
    }


    /**
     * 根据员工获取信息
     */
//    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = {"/", "/{userId}"})
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        String orgId = loginDeptAuth();
        EmployeeVo employeeVo=new EmployeeVo();
        employeeVo.setOrgId(Long.valueOf(orgId));
        employeeVo.setId(userId);
        return employeeService.employeeUnifyGetInfo(employeeVo);
    }


    /**
     * 删除员工
     */
//    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "机构删除员工", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    @Transactional
    public AjaxResult remove(@PathVariable Long[] userIds) {
        String orgId = loginDeptAuth();
        EmployeeVo employeeVo = new EmployeeVo();
        employeeVo.setOrgId(Long.valueOf(orgId));
        if (ArrayUtils.contains(userIds, getUserId())) {
            return error("当前用户不能删除");
        }
        List<Long> longs = Arrays.asList(userIds);
        for (Long userId : longs) {
            if (userId == 1l) {
                continue;
            }
            employeeVo.setId(userId);
            //删除机构用户角色
            employeeService.deleteUnifyRolesOrg(orgId, userId);
            //其他用户存在该用户相关
            UpdateWrapper<SysOrgUser> qwo = new UpdateWrapper<>();
            qwo.eq("user_id", userId);
            qwo.eq("org_id", orgId);
            qwo.set("deleted","1");
            sysOrgUserService.update(qwo);
        }

        return toAjax(true);
    }









//-----------------------------------------   角色管理-----------------------------


    /**
     * 机构可用角色分页
     */
//    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/roles")
    public TableDataInfo list(SysRole sysRole) {
        String orgId = loginDeptAuth();
        sysRole.setOrgId(Long.valueOf(orgId));
        startPage();
        List<SysRole> list = employeeService.employeeRoleList(sysRole);
        return getDataTable(list);
    }

    /**
     * 机构下角色用户
     * */
    @GetMapping("/roleUsers")
    public TableDataInfo roleUserList(SysRole sysRole) {
        String orgId = loginDeptAuth();
        sysRole.setOrgId(Long.valueOf(orgId));
        startPage();
        List<EmployeeVo> list = employeeService.roleUserList(sysRole);
        return getDataTable(list);
    }



//-----------------------------------------   教师管理-----------------------------
    /**
     * 老师管理列表
     */
    @GetMapping("/teacherEmployees")
    public TableDataInfo teacherEmployees(EmployeeVo employeeVo) {
        String orgId = loginDeptAuth();
        employeeVo.setOrgId(Long.valueOf(orgId));
        startPage();
        List<EmployeeVo> list = employeeService.selectTeacherEmployees(employeeVo);
        return getDataTable(list);
    }

    /**
     * 老师管理-当前授课班级
     */
    @GetMapping("/teacherClass")
    public TableDataInfo teacherClass(EmployeeVo employeeVo) {
        String orgId = loginDeptAuth();
        employeeVo.setOrgId(Long.valueOf(orgId));
        startPage();
        List<EmployeeInfoDto> teacherClassList = employeeService.getTeacherClassList(employeeVo);
        return getDataTable(teacherClassList);
    }
}
